package com.qfedu.tantu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.qfedu.tantu.dao.ArticleCommentDao;
import com.qfedu.tantu.dto.ArticleCommentAddDto;
import com.qfedu.tantu.entity.ArticleComment;
import com.qfedu.tantu.service.intf.ArticleCommentService;
import com.qfedu.tantu.vo.R;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;


@Service
public class ArticleCommentServiceImpl implements ArticleCommentService {
    @Resource
    private ArticleCommentDao dao;

    @Override
    public R queryByArticleId(int articleId) {
        //1.校验
        if(articleId>0){
            QueryWrapper<ArticleComment> queryWrapper=new QueryWrapper<>();
            queryWrapper.orderByDesc("create_time");
            queryWrapper.eq("article_id",articleId);

            return R.ok(dao.selectList(queryWrapper));
        }
        return R.fail();
    }

    /*
    * token是用户的id
    * */
    @Override
    public R save(ArticleCommentAddDto dto, int token) {
        //1.校验
        if(dto!=null){
            //
            //2.准备对象
            //MoodComment comment=new MoodComment();
            ArticleComment comment = new ArticleComment();
            BeanUtils.copyProperties(dto,comment);
           /* comment.setCtime(new Date());
            comment.setFlag(1);
            comment.setUid(token);*/
            comment.setUserId(token);
            comment.setCreateTime((new Date()));
            //3.添加数据到数据库
            if(dao.insert(comment)>0){
                //更改 积分

                return R.ok();
            }
        }
        return R.fail();
    }
}
